package com.chuanke.ckfamily.service.finance.impl;

import com.chuanke.ckfamily.dao.base.CkUtil;
import com.chuanke.ckfamily.dao.finance.model.RedPacket;
import com.chuanke.ckfamily.dao.social.model.Family;
import com.chuanke.ckfamily.service.common.CacheService;
import com.chuanke.ckfamily.service.common.impl.RedisLockUtil;
import com.chuanke.ckfamily.service.constant.PacketFinaceType;
import com.chuanke.ckfamily.service.exception.CFException;
import com.chuanke.ckfamily.service.finance.RedPacketService;
import com.chuanke.ckfamily.service.social.FamilyService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chuanke.ckfamily.dao.finance.PacketFinaceDao;
import com.chuanke.ckfamily.dao.finance.model.PacketFinace;
import com.chuanke.ckfamily.service.finance.PacketFinaceService;
import com.weijie.core.BaseBiz;

import java.sql.Timestamp;
import java.util.Date;

@Service
@Transactional(rollbackFor = Exception.class)
public class PacketFinaceServiceImpl extends BaseBiz<PacketFinace, PacketFinaceDao> implements PacketFinaceService {

	@Autowired
	private FamilyService familyService;

	@Autowired
	private RedPacketService redPacketService;

	@Autowired
	private CacheService cacheService;

	@Override
	public void addForCount(Long fid, Long money) {
		// 家族红包增加
		addPacketFinance(fid, null, money, PacketFinaceType.FAMILY_INCOME.getType().intValue(), money);
	}
	@Override
	public void addForSystem(Long fid, Long money) {
		addPacketFinance(fid, null, money, PacketFinaceType.FAMILY_SYSTEM.getType().intValue(), money);
	}

	@Override
	public void sendFamilyPacket(Long fid, Long pid, Long money) throws CFException {
		// 家族红包余额减少
		addPacketFinance(fid, pid, money * -1, PacketFinaceType.FAMILY_SENDREDPACKT.getType().intValue(), null);
	}

	@Override
	public void backFamilyPacket(Long fid, Long pid, Long money) throws CFException {
		Family family = familyService.findById(fid);
		RedPacket redPacket = redPacketService.findById(pid);
		if (family == null || redPacket == null) {
//			throw new CFException(FailCode.APP, "家族或红包不存在");
			return ;
		}

		addPacketFinance(fid, pid, money, PacketFinaceType.FAMILY_REDBACKBACK.getType().intValue(), null);
	}

	private void addPacketFinance(Long fid, Long pid, Long money, Integer type, Long packetIncome) {
		RedisLockUtil redisLock = null;
		try {
			redisLock = cacheService.getLock(CacheService.KEY_PREFIX.packet_finace.name() + fid);
			save(createFinace(fid, pid, money, type));

			Family family = familyService.findById(fid);
			Family f = new Family();
			f.setId(family.getId());
			f.setBalance(CkUtil.sumMoney(family.getBalance(), money));
			if (packetIncome != null) {
				f.setPacket_income_sum(CkUtil.sumMoney(family.getPacket_income_sum(), money));
			}
			familyService.update(f);

		} catch (Exception e) {
			e.printStackTrace();

		} finally {
			if (redisLock != null) {
				redisLock.unlock();
			}

		}
	}

	private PacketFinace createFinace(Long fid, Long pid, Long money, Integer type) {
		PacketFinace packetFinace = new PacketFinace();
		packetFinace.setFid(fid);
		packetFinace.setPid(pid);
		packetFinace.setMoney(money);
		packetFinace.setType(type);
		packetFinace.setCreatetime(new Timestamp(System.currentTimeMillis()));
		packetFinace.setDate(DateFormatUtils.format(new Date(), DateFormatUtils.ISO_DATE_FORMAT.getPattern()));

		return packetFinace;
	}
}